<?php
declare (strict_types=1);

namespace app\command;

use app\common\services\CacheService;
use app\common\services\MailerService;
use app\common\services\WechatService;
use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;
use think\facade\Console;

class Develop extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('develop')
            ->addArgument('option', Argument::OPTIONAL)
            ->addOption('path', null, Option::VALUE_REQUIRED, 'filePath')
            ->setDescription('the develop command');
    }

    protected function execute(Input $input, Output $output)
    {
        $option = trim($input->getArgument('option'));

        switch ($option) {
            case 'createModelDao':
                if (!$input->getOption('path')) {
                    $output->writeln('Path cannot be empty!');
                    return;
                }
                $path = $input->getOption('path');
                $paths = explode('/', $path);

                $modelName = $paths[count($paths) - 1];
                unset($paths[count($paths) - 1]);

                $namespace = $paths ? '\\' . implode('\\', $paths) : '';
                $dir = $paths ? '/' . implode('/', $paths) : '';

                $tableName = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $modelName));

                $this->generateModelFile($dir, $namespace, $modelName, $tableName);
                $this->generateDaoFile($dir, $namespace, $modelName);
                $this->generateRepositoryFile($dir, $namespace, $modelName);

                $output->writeln('Create Success!');
                break;
            case 'clearCache': // 清除缓存
                // 清除所有应用缓存
                CacheService::clearAll();
                CacheService::clearItemAll();
                CacheService::clearSystem();
                $output->writeln('应用缓存清除成功!');
                // 清除数据库缓存
                Console::call('optimize:schema', ['--table', config('database.connections.mysql.database') . '.*']);
                $output->writeln('数据库缓存清除成功!');
                break;
            case 'test': // 测试
                $this->test();
                break;
            default:
                $output->writeln('Option undefined!');
        }
    }

    protected function generateModelFile($dir, $namespace, $modelName, $tableName)
    {
        $dir = app()->getRootPath() . 'app/common/model' . $dir;
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        $tpl = file_get_contents(__DIR__ . '/tpl/BasicModel.tpl');

        $fileContent = str_replace(['{$namespace}', '{$modelName}', '{$tableName}'], [$namespace, $modelName, $tableName], $tpl);

        return file_put_contents($dir . '/' . $modelName . 'Model.php', $fileContent);
    }

    protected function generateDaoFile($dir, $namespace, $modelName)
    {
        $dir = app()->getRootPath() . 'app/common/dao' . $dir;
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        $tpl = file_get_contents(__DIR__ . '/tpl/BasicDao.tpl');

        $fileContent = str_replace(['{$namespace}', '{$modelName}'], [$namespace, $modelName], $tpl);

        return file_put_contents($dir . '/' . $modelName . 'Dao.php', $fileContent);
    }

    protected function generateRepositoryFile($dir, $namespace, $modelName)
    {
        $dir = app()->getRootPath() . 'app/common/repositories' . $dir;
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        $tpl = file_get_contents(__DIR__ . '/tpl/BasicRepository.tpl');

        $fileContent = str_replace(['{$namespace}', '{$modelName}'], [$namespace, $modelName], $tpl);

        return file_put_contents($dir . '/' . $modelName . 'Repository.php', $fileContent);
    }

}
